<!--
*@Author: QWP
*@Description: 收文相关待办
*@Date: 2024-07-14 17:26:27
-->
<template>
  <u-card class="u-dispatch-card-doc"
    :bodyStyle="{ padding: '0 8px' }"
    v-bind="$attrs">
    <u-icon-button type='link' 
      style="position: absolute; right: 12px; top: 8px; z-index: 10"
      @click="roRouter">查看更多
    </u-icon-button>
    <a-tabs v-model:activeKey="activeKey">
      <a-tab-pane key="1" tab="待处理发文" style="padding: 0 0 6px; overflow: hidden;">
        <u-table :columns="dispatchDocCols" 
          :data="stayDealWithDatas" 
          :pagination="false"
          :loading1="loading1"
          class="hide-scrolle-bar f-table-h100">
        </u-table>
      </a-tab-pane>
      <a-tab-pane key="2" tab="待处理收文" style="padding: 0 0 6px; overflow: hidden;">
        <u-table :columns="incomeDocCols" 
          :data="stayCollectDatas" 
          :pagination="false"
          :loading1="loading2"
          class="hide-scrolle-bar f-table-h100">
        </u-table>
      </a-tab-pane>
    </a-tabs>
  </u-card>

  <!-- 发文编辑弹窗 -->
  <DispatchEdit v-model:open="showDiapatchEdit"
    wrap-class-name="full-modal"
    title="发文上报" 
    :current="currntRow"
    @success="showDiapatchEdit = false; getDiapatchDo()"
    @close="currntRow = {}"/>
  
  <!-- 收文编辑 -->
  <IncomeEdit v-model:open="showIncomeEdit"
    wrap-class-name="full-modal"
    title="收文上报" 
    :current="currntRow"
    @success="showIncomeEdit = false; currntRow = {}; getIncomeDo()"
    @close="currntRow = {}"/>
    
</template>

<script lang='ts' setup>
import { ref, reactive, computed, onMounted,  h } from 'vue' 
import { useRoute, useRouter } from 'vue-router'
import { ColProps, FormItemProps, TableColumnProps, Button } from "ant-design-vue";
import dayjs from 'dayjs';
import DispatchEdit from '@/views/oa/officialDoc/dispatch/Edit.vue'
import IncomeEdit from '@/views/oa/officialDoc/incoming/Edit.vue'
import { alertC, messageC } from '@/util/common';
import * as dispatchApi from '@/service/officialDoc/publishDoc/index.ts'
import * as incomeApi from '@/service/officialDoc/incomeDoc'

const router = useRouter();
const activeKey = ref('1');

const currntRow = ref<objetc>({})

// 待处理发文列表
const loading1 = ref<boolean>(false)
const showDiapatchEdit = ref<boolean>(false)
const stayDealWithDatas = ref<any[]>([]);
let dispatchDocCols: Array<TableColumnProps> = reactive([
  { dataIndex: "title", title: "发文标题", width: 150 },
  { dataIndex: "username", title: "拟稿人", width: 100 },
  { dataIndex: "createtime", title: "拟稿时间", width: 100, align: 'center',
    customRender: ({ text }) => (text || '').split(' ')[0]
  },
  { dataIndex: "d", title: "操作", width: 100,
    customRender: ({ text, record }) => {
      return h(Button, { type: 'link',  
        onClick: () => {
          if(record['status'] == 3) {
            collectSort(record, 1)
            return
          }
          currntRow.value = record;
          showDiapatchEdit.value = true
        } 
      }, () => "去处理")
    }
  },
]);

// 待处理收文列表
const loading2 = ref<boolean>(false)
const showIncomeEdit = ref<boolean>(false)
const stayCollectDatas = ref<any[]>([]);
let incomeDocCols: Array<TableColumnProps> = reactive([
  { dataIndex: "incdocunitname", title: "来文单位", width: 150 },
  { dataIndex: "incName", title: "收文编号", width: 100 },
  { dataIndex: "createtime", title: "拟稿时间", width: 100, align: 'center',
    customRender: ({ text }) => (text || '').split(' ')[0]
  },
  { dataIndex: "d", title: "操作", width: 100,
    customRender: ({ text, record }) => {
      currntRow.value = record;
      return h(Button, {  type: 'link',  
        onClick: () => {
          if(record['status'] == 3) {
            collectSort(record, 2)
            return
          }
          currntRow.value = record;
          showIncomeEdit.value = true
        }
      },  () => "去处理")
    }
  },
]);

// 待发文，收文处理
const getDiapatchDo = async () => {
  loading1.value = true;
  try {
    const res = await dispatchApi.queryToDo({ current: 1, size: 1000 })
    if(res.code == 0) {
      const tData = res.data || {}
      stayDealWithDatas.value = tData['1'] || []
      stayCollectDatas.value = tData['2'] || []
    }
  } catch (error) {}
  loading1.value = false;
}

// 待处理收文
const getIncomeDo = async () => {
  loading2.value = true;
  try {
    const res = await incomeApi.queryToDo({ current: 1, size: 1000 })
    if(res.code == 0) {
      // stayCollectDatas.value = res.data || []
    }
  } catch (error) {}
  loading2.value = false;
}

// 发文归档
// type 1 发文归档    2 收文归档
const collectSort = async(record, type) => {
  loading1.value = true;
  try {
    await alertC("确认归档?")
    let res = {}
    if(type == 1) {
      res = await dispatchApi.updateIsFilter(record['keyid'])
      getDiapatchDo()
    } 
    else if(type == 2) {
      res = await incomeApi.updateIsFilter(record['keyid'])
      getIncomeDo()
    }
    if(res.code == 0) {
      messageC(res.msg, "success");
      if(type == 1) {
        getDiapatchDo()
      } 
      else if(type == 2) {
        getIncomeDo()
      }
    }
    else {
      messageC(res.msg, "error");
    }
  } catch (error) {}
  loading1.value = false
}

const roRouter = () => {
  router.push({
    path: activeKey.value == '1' ? '/TransitIndex/DispatchDoc/1' : '/TransitIndex/IncomeDoc/1',
    query: {
      _tab: '1',
      label: activeKey.value == '1' ? '发文拟稿' : '收文登记'
    }
  })
}

onMounted(() => {
  getDiapatchDo()
  // getIncomeDo()
})
</script>

<style lang='scss'>
.u-dispatch-card-doc {
  .ant-card-body {
    height: 100%;
    overflow: hidden !important;
  }
  .ant-tabs-nav {
    &::before {
      border-bottom: none;
    }
    .ant-tabs-ink-bar {
      height: 4px;
      border-radius: 4px 4px 0 0
    }
  }
  .ant-tabs-tab-btn {
    font-size: var(--font-size-level-2);
  }
  .ant-tabs-tab-active {
    font-weight: 600;
  }
  .u-common-table, .ant-spin-nested-loading,  .ant-spin-container,  .ant-table, .ant-table-container {
    height: 100%;
    overflow: hidden;
  }
  .f-table-h100 {
    .ant-table-body {
      max-height: unset !important; 
      height: 100%;
    }
  }
}
</style>